package com.yunupmall.service.admin.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.BrandVo;
import com.yunupmall.api.fordentity.Brand;
import com.yunupmall.mapper.commoditymapper.BrandMapper;
import com.yunupmall.service.admin.service.BrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

@Service
public class Brandimpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {
    @Autowired
    BrandMapper brandMapper;


    /**
     * 分页
     * @param brandVo
     * @return
     */
    @Override
    public Page<Brand> pageDate(BrandVo brandVo) {
        LambdaQueryWrapper<Brand> brandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(brandVo.getName())){
            brandLambdaQueryWrapper.like(Brand ::getName,brandVo.getName());
        }
        if (ObjectUtil.isNotEmpty(brandVo.getId())) {
            brandLambdaQueryWrapper.eq(Brand ::getId,brandVo.getId());
        }
        brandLambdaQueryWrapper.eq(Brand ::getDeflag,0);
        Page<Brand> brandPage = new Page<>(brandVo.getPagePos().getCurrent(),brandVo.getPagePos().getSize());
        return this.page(brandPage,brandLambdaQueryWrapper);
    }

    /**
     * 获取List数据
     * @param brandVo
     * @return
     */
    @Override
    public List<Brand> getList(BrandVo brandVo) {
        LambdaQueryWrapper<Brand> brandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(brandVo.getId())){
            brandLambdaQueryWrapper.eq(Brand ::getId,brandVo.getId());
        }
        if (ObjectUtil.isNotEmpty(brandVo.getName())) {
            brandLambdaQueryWrapper.like(Brand ::getName,brandVo.getName());
        }
        brandLambdaQueryWrapper.eq(Brand::getDeflag,0);
         return this.list(brandLambdaQueryWrapper);
    }

    /**
     * 新增品牌信息
     * @param brandVo
     * @return
     */
    @Override
    public String addData(BrandVo brandVo) {
       if (ObjectUtil.isEmpty(brandVo.getName())) {
        throw new RuntimeException("品牌名称不能为空");
       }
       Brand brand = new Brand();
       BeanUtils.copyProperties(brandVo, brand);
       brand.setCreateTime(LocalDate.now());
       brand.setDeflag(0);
       if (this.save(brand)){
           return "新增成功";
       }else {
           return "新增失败";
       }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Override
    public String deleteDate(BrandVo brandVo) {
        if (ObjectUtil.isEmpty(brandVo.getId())){
            throw new RuntimeException("id为空");
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandVo, brand);
        brand.setDeflag(1);
        if (this.updateById(brand)) {
            return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<Brand> queryBrandData() {
        return brandMapper.queryBrandData();
    }
}
